import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:hex/hex.dart';

import '../my_log_util.dart';

/// 字符串转16进制字符串
String toRadixStr(String data) {
  String res = "";
  for (int i = 0; i < data.length; i++)
    res = res + data.codeUnitAt(i).toRadixString(16);
  return res;
}

//十进制转十进制字符串
String intToHex(int value) {
  return value.toRadixString(16).toUpperCase();
}
//十进制数组转十六进制
String intArrToHex(List<int> backIntValue) {
  String data = "";
  for (var i = 0; i < backIntValue.length; i++) {
    String dataStr = backIntValue[i].toRadixString(16);
    if (dataStr.length < 2) {
      dataStr = "0$dataStr";
    }
    String dataEndStr = dataStr;
    data += dataEndStr;
  }
  return data;
}

/// int数据串转字符
String radixToString(List<int> value) {
  String res = "";
  for (var i = 0; i < value.length; i++) {
    res += String.fromCharCode(int.parse(value[i].toRadixString(10)));
  }
  return res;
}

//转单精度浮点数
double getFloatFromIntBits(String hexString) {
  int intBits = int.parse(hexString, radix: 16);
  final buffer = new ByteData(4);
  buffer.setInt32(0, intBits);
  return buffer.getFloat32(0);
}

// 将十六进制字符串转换为ASC码
String hexToAscii(String hexString) {
  List<int> bytes = [];
  for (int i = 0; i < hexString.length; i += 2) {
    String hex = hexString.substring(i, i + 2);
    bytes.add(int.parse(hex, radix: 16));
  }
  // 将字节列表解码为ASC字符串
  String asciiString = utf8.decode(bytes);
  return asciiString;
}

// ASC码 将转换为十六进制字符串
String asciiToHex(String str) {
  List<int> bytes = utf8.encode(str);
  String hexString = HEX.encode(bytes);
  return hexString;
}

//单精度浮点转hex
String doubleToHex(double cc) {
  Float32List float32List = Float32List(1);
  float32List[0] = cc; // 你的浮点数
  Uint8List uint8List = float32List.buffer.asUint8List();
  String hex = hexEncode(uint8List);
  return reverseHex(hex);
}

//双精度转hex
String doubleToHexString(double value) {
  // 创建一个 Uint8List 视图来存储 double 的位模式
  final bytes = Uint8List(8); // 8 字节用于 double
  final view = Float64List.view(bytes.buffer);
  view[0] = value; // 写入 double 到视图
  // 将字节转换为十六进制字符串
  return bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join('');
}

//十六进制字符串转换为双精度浮点数 (double)
double hexStringToDouble(String hex) {
  // 移除可能存在的前缀 "0x"
  if (hex.startsWith('0x')) {
    hex = hex.substring(2);
  }

  // 计算需要的字节数
  final byteCount = (hex.length + 1) ~/ 2;

  // 创建一个字节列表并填充十六进制数据
  final bytes = Uint8List(byteCount);
  for (var i = 0; i < byteCount; i++) {
    final start = i * 2;
    final end = start + 2;
    bytes[i] = int.parse(hex.substring(start, end), radix: 16);
  }

  // 创建一个 Float64List 视图
  final view = Float64List.view(bytes.buffer);

  // 返回转换后的 double 值
  return view[0];
}

//数据和模校验
bool sumCheck(List<int> data) {
  bool isOk = false;
  int sum = 0;
  for (int i = 0; i < data.length - 1; i++) {
    sum += data[i];
  }
  if (sum % 256 == data.last) {
    isOk = true;
  }
  return isOk;
}

//两字节整型十六进制转int
int twoHexToInt(int a, int b) {
  return a * 256 + b;
}

//生成校验和 计算
int calCheckSum(List<int> data) {
  int sum = 0;
  for (int i = 0; i < data.length; i++) {
    sum += data[i];
  }
  return sum % 256;
}

///十进制转62进制
String intToBase62(int number) {
  const base = 62;
  const chars =
      '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  if (number < base) return chars[number];
  return intToBase62(number ~/ base) + chars[number % base];
}

//十六进制字符串反转
String reverseHex(String hex) {
  return hex.length <= 2
      ? hex
      : reverseHex(hex.substring(2)) + hex.substring(0, 2);
}

String hexEncode(Uint8List uint8List) {
  return uint8List.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join();
}
///患者编号 int转双精度16进制字符
List<int> patientId(int patientId) {
  List<int> res = [];
  String patientIdStr = doubleToHexString(patientId.toDouble());
  MyLogUtil().i("患者十六进制编号：$patientIdStr");
  res = HEX.decode(patientIdStr);
  return res;
}
